<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

class ot_tax {

	function ot_tax() {
		$this->module_key = 'MODULE_ORDER_TOTAL_TAX';
		$this->code = 'ot_tax';
		$this->enabled = ((MODULE_ORDER_TOTAL_TAX_STATUS == 'true') ? true : false);
		$this->sort_order = MODULE_ORDER_TOTAL_TAX_SORT_ORDER;
	}

	function process($params, &$checkout_component)
	{
		$wholesale = $checkout_component->accountInfo['wholesale'];
		$cart = &$checkout_component->cart;
		$cart_prices = &$checkout_component->cart_prices;

		$cartTax = 0.0;
		$sz = sizeof($cart);
		$tax_description = '';
		if (!$wholesale)
		{
			for ($i=0; $i< $sz; $i++)
			{
				$price = (double)$cart[$i]['cart_price'];
				$tax_rate = get_tax_rate($cart[$i]['tax_class_id'], $checkout_component->tax_country_id, $checkout_component->tax_zone_id);
				$item_tax_description = get_tax_description($cart[$i]['tax_class_id'], $checkout_component->tax_country_id, $checkout_component->tax_zone_id);
				if (strlen($item_tax_description) > 0 && $item_tax_description != $tax_description) $tax_description = $item_tax_description;
	
				$cart[$i]['tax_rate'] = $tax_rate;
	
				if ($tax_rate > 0) {
					$cart[$i]['tax'] = $price * ($tax_rate /100);
				}
				else {
					$cart[$i]['tax'] = 0.0;
				}
				$cartTax += ($cart[$i]['tax'] * $cart[$i]['cart_qty']);
			}
		}
		else 
		{
			$cartTax = 0.0;
			$tax_description = 'No Sales Tax For Verified Merchant';
		}
		if ($tax_description == '') $tax_description = "Unknown Tax Rate";
		$cart_prices[$this->code] = array('value' => $cartTax, 'title' => $tax_description . ':', 'class' => $this->code);
	}

	function check() {
		if (!isset($this->_check)) {
			$this->_check = check_module($this->module_key);
		}
		return $this->_check;
	}

	function install() {
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Display Coupon', 'MODULE_ORDER_TOTAL_TAX_STATUS', 'true', 'Do you want to display the tax?', '6', '1','true, false', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_TAX_SORT_ORDER', '20', 'Sort order of display.', '6', '2', now())");
	}

	function remove() {
		remove_module($this->module_key);
	}
}
?>
